/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.naming.spi.DirStateFactory.Result;
import weibo4j.Friendships;
import weibo4j.Users;
import weibo4j.Weibo;
import weibo4j.model.User;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public class constructSQL {

    public static String access_token = Auth.Auth4Code.getCode();

    ;
    public static Users um = new Users();

    public static void main(String[] args) throws WeiboException, SQLException {
        prepareWeibo();
        //getFriends();
        getNameByID("1778830754");
    }
    public static void getNameByID(String uid) throws SQLException{
        Database conn = Database.getInstance();
        String sql="select * from friendship where `uid`=12";
        List<Map<String, Object>> result= conn.query(sql);
        System.out.println(result.get(1).get("cid"));
        System.out.println(result.get(1).get("fid"));
        System.out.println(result.get(0).get("fid"));
        System.out.println(result.get(0).get("cid"));
    }
    public static void prepareWeibo() {

        System.out.println("Access token is: " + access_token);
        Weibo weibo = new Weibo();
        weibo.setToken(access_token);
    }

    public static void getFriends() throws WeiboException, SQLException {
        String uid = Config.uid;
        User user = um.showUserById(uid);
        Friendships fm = new Friendships();


        int friendsCount = user.getFriendsCount();
        int page_count = 200;
        int all_pages = friendsCount / page_count;
        List<String> friendsIDs = new ArrayList<String>();

        for (int i = 0; i < all_pages + 1; i++) {
            String f[] = fm.getFriendsIds(uid, page_count, i * page_count);
            friendsIDs.addAll(Arrays.asList(f));
        }

        String username = "";
        int i = 0;
        for (String s : friendsIDs) {
            System.out.println((++i) + "/" + friendsCount);
            username = "";
            try {
                user = um.showUserById(s);
                username = user.getName();
            } catch (Exception e) {
            }
            String sql = "insert into `weibonews`.`id2name` (`uid`, `name`) VALUES (";
            String values = "\'" + s + "\',\'" + username + "\')";
            String statement = sql + values;
            Database conn = Database.getInstance();
            String querystr = "select count(*) from id2name where `uid`="
                    + "\'" + s + "\'";
            if (conn.count(querystr) == 0) {
                conn.insert(statement);
            }
        }
    }
}
